package com.example.yls.demoa;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;

/**
 * Created by My Family on 2018/5/28.
 */

public class SlidingMenu extends HorizontalScrollView {
    private  int mMenuWidth;
    private View mMenuView;
    private ViewGroup mContentView;
    private GestureDetector mGestureDetector;
    private boolean mMenuIsOpen;
    private ImageView mShadowIv;
    private Context mContext;

    public SlidingMenu(Context context) {
        super(context);
    }

    public SlidingMenu(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SlidingMenu(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        // 获取自定义的右边留出的宽度
        TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.SlidingMenu);
        float rightPadding = array.getDimension(
                R.styleable.SlidingMenu_rightPadding,dip2px(50));
        // 计算菜单的宽度 = 屏幕的宽度 - 自定义右边留出的宽度
        mMenuWidth = (int) (getScreenWidth() - rightPadding);
        array.recycle();

        mGestureDetector = new GestureDetector(context,new GestureListener());
        this.mContext = context;
    }

    /**
     * 把dip 转成像素
     */
    private float dip2px(int dip) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dip,getResources().getDisplayMetrics());
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        ViewGroup container = (ViewGroup) getChildAt(0);

        int containerChildCount = container.getChildCount();
        if(containerChildCount > 2){
            throw new IllegalStateException("SlidingMenu 根布局LinearLayout下面只允许两个菜单布局");
        }
        mMenuView = container.getChildAt(0);

        mContentView =new FrameLayout(mContext);
        ViewGroup.LayoutParams contentParams = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT
        );
        View oldContentView = container.getChildAt(1);
        container.removeView(oldContentView);

        mContentView.addView(oldContentView);

        mShadowIv = new ImageView(mContext);
        mShadowIv.setBackgroundColor(Color.parseColor("#99000000"));
        mContentView.addView(mShadowIv);

        container.addView(mContentView);

        mMenuView.getLayoutParams().width = mMenuWidth;
        mContentView.getLayoutParams().width = getScreenWidth();
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if(mGestureDetector.onTouchEvent(ev)){
            return mGestureDetector.onTouchEvent(ev);
        }
        switch (ev.getAction()){
            case MotionEvent.ACTION_UP:
                int currentScrollX = getScrollX();
                if(currentScrollX > mMenuWidth / 2){
                    closeMenu();
                }else {
                    openMenu();
                }
                return false;
        }

        return super.onTouchEvent(ev);
    }

    private void openMenu() {
        smoothScrollTo(0,0);
        mMenuIsOpen = true;
    }

    private void closeMenu() {
        smoothScrollTo(mMenuWidth,0);
        mMenuIsOpen = false;
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if(changed){
            scrollTo(mMenuWidth,0);
        }
    }
    /**
     * 获取屏幕的宽度
     */
    public int getScreenWidth() {
        Resources resources = getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        return dm.widthPixels;
    }

    private class GestureListener implements GestureDetector.OnGestureListener {
        @Override
        public boolean onDown(MotionEvent motionEvent) {
            return false;
        }

        @Override
        public void onShowPress(MotionEvent motionEvent) {

        }

        @Override
        public boolean onSingleTapUp(MotionEvent motionEvent) {
            return false;
        }

        @Override
        public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
            return false;
        }

        @Override
        public void onLongPress(MotionEvent motionEvent) {

        }

        @Override
        public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float x, float y) {
            Log.e("TAG",x + "");
            if(mMenuIsOpen){
                if(x < -500){
                    toggleMenu();
                    return true;
                }else {
                    if(x > 500);{
                        toggleMenu();
                        return true;
                    }
                }
            }
            return false;
        }
    }

    private void toggleMenu() {
        if(mMenuIsOpen){
            closeMenu();
        }else {
            openMenu();
        }
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);

        Log.e("TAG",1+"");
        mMenuView.setTranslationX(1 * 08f);

        // 7.给内容添加阴影效果 - 计算梯度值
        float gradientValue = l * 1f / mMenuWidth;// 这是  0 - 1 变化的值

        // 7.给内容添加阴影效果 - 给阴影的View指定透明度   0 - 1 变化的值
        float shadowAlpha = 1 - gradientValue;
        mShadowIv.setAlpha(shadowAlpha);
    }
}
